﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace AutomationFramework
{
    public class ApiMapping
    {
        public ApiMapping(string xmlData)
        {
            if (string.IsNullOrEmpty(xmlData))
            {
                throw new ArgumentNullException("xmlData", "xmlData cannot be null or empty");
            }

            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(xmlData);
            ControlName = xDoc.SelectSingleNode("ApiMapping/Control/Name").InnerText;
            ControlClassName = xDoc.SelectSingleNode("ApiMapping/Control/ClassName").InnerText;
            ControlLocalizedControlTypeProperty = xDoc.SelectSingleNode("ApiMapping/Control/LocalizedControlTypeProperty").InnerText;
            ControlProviderDescriptionId = xDoc.SelectSingleNode("ApiMapping/Control/ProviderDescriptionId").InnerText;
            ControlHelpTextProperty = xDoc.SelectSingleNode("ApiMapping/Control/HelpTextProperty").InnerText;
            ControlItems = xDoc.SelectSingleNode("ApiMapping/Control/Items").InnerText;

            XmlNode node = xDoc.SelectSingleNode("ApiMapping/Control/ElementProviderFromPoint");
            if (node != null)
            {
                ControlElementProviderFromPointAvailable = Convert.ToBoolean(node.Attributes["available"].Value);
                ControlElementProviderFromPoint = node.InnerText;
            }

            node = xDoc.SelectSingleNode("ApiMapping/Control/IValueProvider");
            if (node != null)
            {
                ControlIValueProviderAvailable = Convert.ToBoolean(node.Attributes["available"].Value);
                if (ControlIValueProviderAvailable)
                {
                    ControlIValueProviderIsReadOnly = Convert.ToBoolean(node.SelectSingleNode("IsReadOnly").InnerText);
                    ControlIValueProviderSetValue = node.SelectSingleNode("SetValue").InnerText;
                    ControlIValueProviderValue = node.SelectSingleNode("Value").InnerText;
                }
            }
            node = xDoc.SelectSingleNode("ApiMapping/Control/IExpandCollapseProvider");
            if (node != null)
            {
                ControlIExpandCollapseProviderAvailable = Convert.ToBoolean(node.Attributes["available"].Value);
                if (ControlIExpandCollapseProviderAvailable)
                {
                    ControlIExpandCollapseCollapse = node.SelectSingleNode("Collapse").InnerText;
                    ControlIExpandCollapseExpand = node.SelectSingleNode("Expand").InnerText;
                    ControlIExpandCollapseState = node.SelectSingleNode("ExpandCollapseState").InnerText;
                }
            }

            node = xDoc.SelectSingleNode("ApiMapping/Control/IToggleProvider");
            if (node != null)
            {
                ControlIToggleProviderAvailable = Convert.ToBoolean(node.Attributes["available"].Value);
                if (ControlIToggleProviderAvailable)
                {
                    ControlIToggleProviderToggle = node.SelectSingleNode("Toggle").InnerText;
                    ControlIToggleProviderToggleState = node.SelectSingleNode("ToggleState").InnerText;
                }
            }

            node = xDoc.SelectSingleNode("ApiMapping/Control/IInvokeProvider");
            if (node != null)
            {
                ControlIInvokeProviderAvailable = Convert.ToBoolean(node.Attributes["available"].Value);
                if (ControlIInvokeProviderAvailable)
                {
                    ControlIInvokeProviderInvoke = node.SelectSingleNode("Invoke").InnerText;
                }
            }


            ObjectName = xDoc.SelectSingleNode("ApiMapping/Object/Name").InnerText;
            ObjectClassName = xDoc.SelectSingleNode("ApiMapping/Object/ClassName").InnerText;
            ObjectLocalizedControlTypeProperty = xDoc.SelectSingleNode("ApiMapping/Object/LocalizedControlTypeProperty").InnerText;
            ObjectProviderDescriptionId = xDoc.SelectSingleNode("ApiMapping/Object/ProviderDescriptionId").InnerText;
            ObjectHelpTextProperty = xDoc.SelectSingleNode("ApiMapping/Object/HelpTextProperty").InnerText;
            ObjectItems = xDoc.SelectSingleNode("ApiMapping/Object/Items").InnerText;
            ObjectBoundingRectangle = xDoc.SelectSingleNode("ApiMapping/Object/BoundingRectangle").InnerText;

            node = xDoc.SelectSingleNode("ApiMapping/Object/IValueProvider");
            if (node != null)
            {
                ObjectIValueProviderAvailable = Convert.ToBoolean(node.Attributes["available"].Value);
                if (ObjectIValueProviderAvailable)
                {
                    ObjectIValueProviderIsReadOnly = Convert.ToBoolean(node.SelectSingleNode("IsReadOnly").InnerText);
                    ObjectIValueProviderSetValue = node.SelectSingleNode("SetValue").InnerText;
                    ObjectIValueProviderValue = node.SelectSingleNode("Value").InnerText;
                }
            }
            node = xDoc.SelectSingleNode("ApiMapping/Object/IExpandCollapseProvider");
            if (node != null)
            {
                ObjectIExpandCollapseProviderAvailable = Convert.ToBoolean(node.Attributes["available"].Value);
                if (ObjectIExpandCollapseProviderAvailable)
                {
                    ObjectIExpandCollapseCollapse = node.SelectSingleNode("Collapse").InnerText;
                    ObjectIExpandCollapseExpand = node.SelectSingleNode("Expand").InnerText;
                    ObjectIExpandCollapseState = node.SelectSingleNode("ExpandCollapseState").InnerText;
                }
            }

            node = xDoc.SelectSingleNode("ApiMapping/Object/IToggleProvider");
            if (node != null)
            {
                ObjectIToggleProviderAvailable = Convert.ToBoolean(node.Attributes["available"].Value);
                if (ObjectIToggleProviderAvailable)
                {
                    ObjectIToggleProviderToggle = node.SelectSingleNode("Toggle").InnerText;
                    ObjectIToggleProviderToggleState = node.SelectSingleNode("ToggleState").InnerText;
                }
            }

            node = xDoc.SelectSingleNode("ApiMapping/Object/IInvokeProvider");
            if (node != null)
            {
                ObjectIInvokeProviderAvailable = Convert.ToBoolean(node.Attributes["available"].Value);
                if (ObjectIInvokeProviderAvailable)
                {
                    ObjectIInvokeProviderInvoke = node.SelectSingleNode("Invoke").InnerText;
                }
            }
        }

        public string ControlName
        {
            get;
            private set;
        }

        public string ControlClassName
        {
            get;
            private set;
        }

        public string ControlLocalizedControlTypeProperty
        {
            get;
            private set;
        }

        public string ControlProviderDescriptionId
        {
            get;
            private set;
        }

        public string ControlHelpTextProperty
        {
            get;
            private set;
        }

        public string ControlItems
        {
            get;
            private set;
        }

        public bool ControlElementProviderFromPointAvailable { get; private set; }

        public string ControlElementProviderFromPoint { get; private set; }

        public bool ControlIExpandCollapseProviderAvailable { get; private set; }

        public string ControlIExpandCollapseCollapse { get; private set; }

        public string ControlIExpandCollapseExpand { get; private set; }

        public string ControlIExpandCollapseState { get; private set; }

        public bool ControlIValueProviderAvailable { get; private set; }

        public bool ControlIValueProviderIsReadOnly { get; private set; }

        public string ControlIValueProviderSetValue { get; private set; }

        public string ControlIValueProviderValue { get; private set; }

        public bool ControlIToggleProviderAvailable { get; private set; }

        public string ControlIToggleProviderToggleState { get; private set; }

        public string ControlIToggleProviderToggle { get; private set; }

        public bool ControlIInvokeProviderAvailable { get; private set; }

        public string ControlIInvokeProviderInvoke { get; private set; }

        public string ObjectName
        {
            get;
            private set;
        }

        public string ObjectClassName
        {
            get;
            private set;
        }

        public string ObjectLocalizedControlTypeProperty
        {
            get;
            private set;
        }

        public string ObjectProviderDescriptionId
        {
            get;
            private set;
        }

        public string ObjectHelpTextProperty
        {
            get;
            private set;
        }

        public string ObjectItems
        {
            get;
            private set;
        }

        public string ObjectBoundingRectangle
        {
            get;
            private set;
        }

        public bool ObjectIExpandCollapseProviderAvailable { get; private set; }

        public string ObjectIExpandCollapseCollapse { get; private set; }

        public string ObjectIExpandCollapseExpand { get; private set; }

        public string ObjectIExpandCollapseState { get; private set; }

        public bool ObjectIValueProviderAvailable { get; private set; }

        public bool ObjectIValueProviderIsReadOnly { get; private set; }

        public string ObjectIValueProviderSetValue { get; private set; }

        public string ObjectIValueProviderValue { get; private set; }

        public bool ObjectIToggleProviderAvailable { get; private set; }

        public string ObjectIToggleProviderToggleState { get; private set; }

        public string ObjectIToggleProviderToggle { get; private set; }

        public bool ObjectIInvokeProviderAvailable { get; private set; }
        
        public string ObjectIInvokeProviderInvoke { get; private set; }

    }
}
